#!usr/bin/env python
# *- coding:utf-8 -*-
# Andy Create @ 11/8/2019 9:28 PM

from rest_framework import serializers

from api.models import *


class ArticleSerializer(serializers.ModelSerializer):
    # 文章列表
    author = serializers.CharField(source='author.username', read_only=True)
    date = serializers.SerializerMethodField()
    content = serializers.CharField(source='articledetail.content', read_only=True)

    def get_date(self, obj):
        # format date field
        return obj.date.strftime("%Y-%m-%d")

    class Meta:
        model = Article
        fields = '__all__'


class PostArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        exclude = ['author', ]


class FullArticleSerializer(serializers.ModelSerializer):
    author = serializers.CharField(source='author.username')
    date = serializers.SerializerMethodField()
    content = serializers.CharField(source='articledetail.content')
    comment = serializers.SerializerMethodField()

    def get_date(self, obj):
        # format date field
        return obj.date.strftime("%Y-%m-%d")

    def get_comment(self, obj):
        return Comment.objects.filter(article=obj).values()

    class Meta:
        model = Article
        fields = '__all__'


class ArticleDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = ArticleDetail
        fields = ['content', ]


class CommentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment
        fields = '__all__'


class PostCommentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment
        fields = ['content', ]
